{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fc98918d",
   "metadata": {},
   "source": [
    "* 学习人：黄嘉欣\n",
    "* 时间：2021-11-15\n",
    "* week12\n",
    "* 学习线上资源：[python官方文档](https://python.org/doc)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5d1856e",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> 1. 函数的定义：def\n",
    "> 2. 函数名的定义规则-->变量名一样的规则\n",
    "> 3. 调用函数的方式\n",
    "> 4. 参数：\n",
    ">> A. 形式参数（定义函数）\n",
    ">> B. 实际参数（调用函数）\n",
    ">> C. 有多个参数时：\n",
    ">>> 1. 关键字参数\n",
    ">>> 2. 位置参数(调用函数时)-->关键字参数要均放在末尾（多个不考虑顺序）\n",
    ">>> 3. 默认参数(定义函数时)-->默认参数放在末尾（多个不考虑顺序）\n",
    "\n",
    "# 本周知识\n",
    "> 1. 函数的返回值\n",
    "> 2. 函数的参数类型\n",
    "> 3. 模块与函数的关系\n",
    "> 4. 定义一个模块，然后调用模块\n",
    ">> A. 调用模块\n",
    ">> B. 调用模块中的函数\n",
    "> 5. 模块与函数实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f5708a3",
   "metadata": {},
   "source": [
    "### 1. 函数的返回值\n",
    "> 1. 函数一定有返回值（无论是什么编程语言）\n",
    "> 2. 在其他的编程语言中（如c语言，java等），返回值一定要写，不写会报错！！对于python（更高级的语言），返回值如果不写，给定默认值：None\n",
    "> 3. 返回值一般为函数体运行的结果（result）,也可以是自定义的函数返回值()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "81c37dc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践1\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回完整的姓名\"\"\"\n",
    "    full_name = first_name + ' ' + last_name\n",
    "    return full_name\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "db4ccf02",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Li Hua\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Li\",\"Hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d94390b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"Lihua\" and password == 123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4a896e11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"Lihua\",12345)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71e60b65",
   "metadata": {},
   "source": [
    "## 2.函数的参数\n",
    "> 1. 函数输入的参数\n",
    ">> 1. 函数可以输入复杂的参数:\n",
    ">>> A. 函数参数为列表\n",
    ">>> B. 函数参数为字典\n",
    "> 2. 函数可以输入不确定个数的参数：\n",
    ">> A. *args\n",
    ">> B. **kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2ccfee24",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"测试输入参数为列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0d348dd1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['lihua', 'xiaojie', 'xiaowei']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"lihua\",\"xiaojie\",\"xiaowei\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3f0be26c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict_test(request_header):\n",
    "    \"\"\"测试输入参数为字典\"\"\"\n",
    "    print(request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2b15f6ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "httpbin_get = {\n",
    "      \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61adb9c3-46fca34d01ac274118319400\"\n",
    "  }, \n",
    "  \"origin\": \"61.242.54.94\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d9d3321f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6', 'Host': 'httpbin.org', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43', 'X-Amzn-Trace-Id': 'Root=1-61adb9c3-46fca34d01ac274118319400'}\n"
     ]
    }
   ],
   "source": [
    "dict_test(httpbin_get['headers'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c13e85ee",
   "metadata": {},
   "source": [
    "> 2. 函数可以输入不确定个数的参数：\n",
    ">> A. *args\n",
    ">>> 1. args 可以接受多个数据（单值）\n",
    ">>> 2. args 将多个数据存在一个**元组**当中，实际还是一个变量\n",
    ">> B. **kwargs\n",
    ">>> 1. kwargs 可以接受多个数据（键值对）==> 关键字参数\n",
    ">>> 2. kwargs 将"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "9336cd9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# *args\n",
    "def food_ingred(food,*ingreds):\n",
    "    \"\"\"食物配料单\"\"\"\n",
    "    return food,ingreds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "456cbe30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food_ingred(\"pizza\",\"mushroom\",\"green peppers\",\"extra cheese\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "986e5433",
   "metadata": {},
   "outputs": [],
   "source": [
    "# **kwargs\n",
    "def person(stu_name,**stu_info):\n",
    "    \"\"\"学生个人信息完善表\"\"\"\n",
    "    return stu_name,stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "67b55c4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Lihua', {'age': 18, 'job': 'student', 'address': '广州南方学院'})"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person(\"Lihua\",age=18,job=\"student\",address=\"广州南方学院\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18fc97bb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a338bf29",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "9b3af70b",
   "metadata": {},
   "source": [
    "### 2.1. 函数输出的返回值\n",
    "> 1. 返回值可以是字符串、数值、列表、字典等数据（单一值）\n",
    "> 2. 返回值是唯一的，不可以有两个（这是其他语言，如：c语言，java等）\n",
    "> 3. python可以输出多个返回值，结果会用一个元组进行封装并返回（本质不违背函数的定义）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "59084aa8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e522af66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f11b82bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return[1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "913d1e2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d5d420b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return httpbin_get['headers']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "30faff7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',\n",
       " 'Accept-Encoding': 'gzip, deflate',\n",
       " 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',\n",
       " 'Host': 'httpbin.org',\n",
       " 'Upgrade-Insecure-Requests': '1',\n",
       " 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43',\n",
       " 'X-Amzn-Trace-Id': 'Root=1-61adb9c3-46fca34d01ac274118319400'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "a1cd13ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "1c42f437",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f776580f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试多个返回值\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return 1,2,3,\"li hua\",[4,5,6],{\"name\":\"jiaxin\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8509918b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3, 'li hua', [4, 5, 6], {'name': 'jiaxin'})"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "067ecedb",
   "metadata": {},
   "source": [
    "# 3.模块\n",
    "> 1. 使用模块可以隐藏函数的源代码\n",
    "> 2. 使用的人不需要会底层代码，（只需要会使用模块即可）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "77d9cd2d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (study.py, line 3)",
     "output_type": "error",
     "traceback": [
      "Traceback \u001b[1;36m(most recent call last)\u001b[0m:\n",
      "  File \u001b[0;32m\"D:\\pra\\Anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py\"\u001b[0m, line \u001b[0;32m3437\u001b[0m, in \u001b[0;35mrun_code\u001b[0m\n    exec(code_obj, self.user_global_ns, self.user_ns)\n",
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-13-ab8b37a5fa59>\"\u001b[1;36m, line \u001b[1;32m1\u001b[1;36m, in \u001b[1;35m<module>\u001b[1;36m\u001b[0m\n\u001b[1;33m    import study\u001b[0m\n",
      "\u001b[1;36m  File \u001b[1;32m\"D:\\网络与新媒体\\jupyter_notebook\\study.py\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    return set（\"aeiou\"）& set(word)\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "# 调用模块中函数的方法一： 调用整个模块\n",
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "662c57ed",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'study' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-e29f9709fe97>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstudy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearch4vowels\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"hello jiaxin\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'study' is not defined"
     ]
    }
   ],
   "source": [
    "study.search4vowels(\"hello jiaxin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "452d9b7f",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (study.py, line 3)",
     "output_type": "error",
     "traceback": [
      "Traceback \u001b[1;36m(most recent call last)\u001b[0m:\n",
      "  File \u001b[0;32m\"D:\\pra\\Anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py\"\u001b[0m, line \u001b[0;32m3437\u001b[0m, in \u001b[0;35mrun_code\u001b[0m\n    exec(code_obj, self.user_global_ns, self.user_ns)\n",
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-14-b5b36b0f3bac>\"\u001b[1;36m, line \u001b[1;32m2\u001b[1;36m, in \u001b[1;35m<module>\u001b[1;36m\u001b[0m\n\u001b[1;33m    from study import search4vowels\u001b[0m\n",
      "\u001b[1;36m  File \u001b[1;32m\"D:\\网络与新媒体\\jupyter_notebook\\study.py\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    return set（\"aeiou\"）& set(word)\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "# 调用模块中函数的方法2，调用模块中某一个函数\n",
    "from study import search4vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e0279e69",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'search4vowels' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-95971df8f403>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0msearch4vowels\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"djbasnad\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'search4vowels' is not defined"
     ]
    }
   ],
   "source": [
    "search4vowels(\"djbasnad\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "614cdbf3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a716979",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "572d4d03",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39b2362f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92268683",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e15ef23",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fc14878",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06d63709",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "949d1f64",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
